import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import "rxjs/add/operator/map";
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import {URLSearchParams} from "@angular/http";
import {HttpURLService} from "../../../httpURL.service";
@Injectable()
export class EnvironService {
  public emisurl:string = this.httpURLService.getUrl("emis");
  private listUrl =this.emisurl+ '/epa/queryEpaListByPage';//获取列表
  private deleteUrl = this.emisurl+'/epa/deleteEpa';//删除接口
  private addUrl =this.emisurl+ '/epa/addEpa';//添加接口
  private editUrl = this.emisurl+'/epa/updateEpa';//编辑接口
  private areaUrl = this.emisurl+'/baseAdminArea/queryAreaList';//行政区接口
  private userUrl = this.emisurl+'/purview/queryPurviewUserType';//用户接口
  constructor(private http: HttpClient,
              private httpURLService: HttpURLService) { }

  getArea(): Observable<any> {
    return this.http.get(`${this.areaUrl}`)
      .map(data => data)
      .catch(this.handleError);
  }
  getUser(): Observable<any> {
    return this.http.get(`${this.userUrl}`)
      .map(data => data)
      .catch(this.handleError);
  }
//查询列表
  getExhibits(queryData): Observable<any> {
    return this.http.get(`${this.listUrl}?${queryData}`)
      .map(data => data)
      .catch(this.handleError);
  }
  //删除
  deletePollut(id): Observable<any> {
    return this.http.get(`${this.deleteUrl}/${id}`)
      .map(data =>data)
      .catch(this.handleError);
  }
  //添加
  createPollut(data): Observable<any> {
    return this.http.post(`${this.addUrl}`, data)
      .map(data =>data)
      .catch(this.handleError);
  }
  editExhibit(data): Observable<any> {
    return this.http.post(`${this.editUrl}`, data)
      .map(data =>data)
      .catch(this.handleError);
  }
  private handleError(error: Response | any) {
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    return Observable.throw(errMsg);
  }
}
